package me.keiwu.admin.controller;

import com.alibaba.fastjson.JSON;
import me.keiwu.admin.domain.system.SysInviteCode;
import me.keiwu.admin.domain.system.SysUser;
import me.keiwu.admin.job.WXTemplateMsgJob;
import me.keiwu.admin.service.UserService;
import me.keiwu.admin.util.bean.AuthorityConst;
import me.keiwu.pub.data.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by kei on 31/10/2017.
 */
@Controller
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private WXTemplateMsgJob wxTemplateMsgJob;

    @RequestMapping("/page/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/register")
    @ResponseBody
    public Response register(String username, String password, String inviteCode,
                             String firstName, String lastName, String email, String imageUrl) {
        SysInviteCode code = userService.verifyInviteCode(inviteCode);
        if (code == null) return Response.fail("wrong invite code!");
        SysUser user = userService.register(username, password, code.getType(), firstName, lastName, email, imageUrl);
        if (user == null) return Response.fail("register fail!");
        return Response.ok(user.getId());
    }


    @PreAuthorize("hasAuthority('" + AuthorityConst.ADMIN + "')")
    @RequestMapping("/invite/code/add")
    @ResponseBody
    public Response addInviteCode(String inviteCodes, Integer type) {
        List<String> codes = JSON.parseArray(inviteCodes, String.class);
        userService.addInviteCodes(codes, type);
        return Response.ok();
    }

    @RequestMapping("/testLonginHint")
    @ResponseBody
    public Response testLonginHint(){
        wxTemplateMsgJob.runLoginHint();
        return Response.ok();
    }
    @RequestMapping("/testGameResult")
    @ResponseBody
    public Response testGameResult(){
        wxTemplateMsgJob.challengeResultNoticeJob();
        return Response.ok();
    }
    @RequestMapping("/testGameInvite")
    @ResponseBody
    public Response testGameInvite(){
        wxTemplateMsgJob.challengeInviteNoticeFirstJob();
        return Response.ok();
    }


}
